<?php

namespace IDock\Insurance\JTIC\Response;

use IDock\Http\Response;
use IDock\IDock\IDockEnums;
use IDock\Insurance\JTIC\JTICEnums;
use IDock\Printer\NonCar\NonCarInquiryPrinter;
use IDock\Printer\NonCar\NonCarInquirySinglePrinter;
use IDock\Printer\Premium\PremiumBIPrinter;
use IDock\Printer\Premium\PremiumCoveragePrinter;
use IDock\Printer\Premium\PremiumInquiryPrinter;

class PremiumInquiryResponse extends AllJTICResponse
{

    /**
     * @var PremiumInquiryPrinter $printer
     * @var  $printer
     */
    public $printer = PremiumInquiryPrinter::class;

    protected $records = [];

    protected $result = [];

    public $planCode = '';

    public $productCode = '';

    public $insuredNum = 1;

    public function printerInit(): Response
    {
        if (!$this->printer->success) {
            return $this;
        }
        //询价流程号
        $data = $this->getBody();
        $ciData = $data['ApplicationTCI'] ?? [];
        $biData = $data['ApplicationVCI'] ?? [];
        $baseData = $data['BasePart'] ?? [];
        $reInsure = $data['ReInsure'] ?? [];
        if (!empty($reInsure)) {
            $ciMessage = $reInsure['TCIReMessage'] ?? '';
            $biMessage = $reInsure['VCIReMessage'] ?? '';
            $this->getRepeatData($ciMessage, $biMessage);
            if ($this->printer->repeatInsuredFlg) {
                $this->printer->success = false;
                $this->printer->repeatMessage = $ciMessage . "\n" . $biMessage;
                return $this;
            }
            $this->setError($ciMessage . "\n" . $biMessage);
            return $this;
        }
        $this->printer->inquiryNo = $baseData['QtnId'] ?? '';
        $this->printer->argc['QtnId'] = $baseData['QtnId'] ?? '';
        $this->printer->argc['premium'] = $baseData['SumPrm'] ?? '';
        //车船税
        if ($carShipTax = $ciData['VehicleTax'] ?? '') {
            $this->printer->carTaxPrinter->carShipTax = $carShipTax['SumTax'] ?? '';
            $this->printer->carTaxPrinter->overdue = $carShipTax['SumOverdue'] ?? '';
            $this->printer->carTaxPrinter->payBackTax = $carShipTax['SumTaxDefault'] ?? '';
            $this->printer->carTaxPrinter->thisYearTax = $carShipTax['AnnualTaxDue'] ?? '';
        }

        //商业险
        if ($biData) {
            $this->printer->biPremiumPrinter = new PremiumBIPrinter();
            $this->printer->biPremiumPrinter->biPremium = $biData['Premium'] ?? '';
            $this->printer->biPremiumPrinter->biBasePremium = $biData['Premium'] ?? '';
            $this->printer->ncdFactor = $biData['ClaimAdjustValue'] ?? '';

            //商业险系数
            $this->printer->premiumFactorPrinter->VCIFactorAppend('自主定价系数', 'selfPricingCoef', $biData['SelfPricingCoef'] ?? '');
            $this->printer->premiumFactorPrinter->VCIFactorAppend('无赔款优待等级', 'ClaimAdjustLevel', $biData['ClaimAdjustLevel'] ?? '');
            $this->printer->premiumFactorPrinter->VCIFactorAppend('连续承保年数', 'InsureYears', $biData['InsureYears'] ?? '');
            $this->printer->premiumFactorPrinter->VCIFactorAppend('连续承保期间出险次数', 'ClaimTimes', $biData['ClaimTimes'] ?? '');
//            $this->printer->premiumFactorPrinter->VCIFactorAppend('连续承保年数', 'insureYears', $biData['insureYears'] ?? '');
//            $this->printer->premiumFactorPrinter->VCIFactorAppend('连续承保期间出险次数', 'insureYears', $biData['claimTimes'] ?? '');
//            $this->printer->premiumFactorPrinter->VCIFactorAppend('无赔优系数级别', 'claimAdjustLevel', $biData['claimAdjustLevel'] ?? '');
            //忠诚度
            $loyalty = '新';
            if($biData['RenewalFlag'] ?? ''){
                $loyalty = '转';
            }
            if($biData['RenewalInsFlag'] ?? ''){
                $loyalty = '续';
            }
            $this->printer->premiumFactorPrinter->ComFactorAppend('商业险忠诚度', 'BIRenewalType', $loyalty);
        }

        //交强险
        if ($ciData) {
            $this->printer->ciPremiumPrinter->ciPremium = $ciData['Premium'] ?? '';
            $this->printer->ciPremiumPrinter->ciBasePremium = $ciData['Premium'] ?? '';

//            $this->printer->premiumFactorPrinter->TCIFactorAppend('理赔调整系数', 'claimAdjustValue', $ciData['claimAdjustValue'] ?? '');
//            $this->printer->premiumFactorPrinter->TCIFactorAppend('违法浮动原因代码', 'peccancyAdjustReason', $ciData['peccancyAdjustReason'] ?? '');
//            $this->printer->premiumFactorPrinter->TCIFactorAppend('违法调整系数', 'peccancyAdjustValue', $ciData['peccancyAdjustValue'] ?? '');
            $this->printer->premiumFactorPrinter->ComFactorAppend('UBI分档', 'UBITranche', $baseData['UBITranche'] ?? '');
            $this->printer->premiumFactorPrinter->ComFactorAppend('承保分', 'UnderwritingScore', $baseData['UnderwritingScore'] ?? '');
            //忠诚度
            $loyalty = '新';
            if($ciData['RenewalFlag'] ?? ''){
                $loyalty = '转';
            }
            if($ciData['RenewalInsFlag'] ?? ''){
                $loyalty = '续';
            }
            $this->printer->premiumFactorPrinter->ComFactorAppend('交强险忠诚度', 'CIRenewalType', $loyalty);
        }
        $coverageList = $biData['CoverageList']['CoverageItem'] ?? [];
        //险种
        if ($coverageList) {
            foreach ($coverageList as $kind) {
                $coverage = new PremiumCoveragePrinter();
                $coverage->coverageCode = array_search($kind['CoverageCode'], JTICEnums::$coverageCode);
                $coverage->coverageName = IDockEnums::$coverageCode[$coverage->coverageCode] ?? '';
                $coverage->premium = $kind['CoveragePremium'] ?? '';
                $coverage->basePremium = $kind['CoverageAmount'] ?? '';
                $coverage->amount = $kind['CoverageAmount'] ?? '';

                $this->printer->coverageListAppend($coverage);
            }
        }
        //非车信息
        return $this;
    }

    /**
     * 重复投保
     * @param $ciMeg
     * @param $biMeg
     * @return void
     */
    public function getRepeatData($ciMeg, $biMeg)
    {
        if ($ciMeg and preg_match('/终保日期 ([\d\s:-]+)/', $ciMeg, $matches)) {
            if ($matches[1] ?? '') {
                $ciStartData = date('Y-m-d H:i:s', strtotime($matches[1]));
                $this->printer->repeatInsurePrinter['ciStartData'] = $ciStartData;
                $this->printer->repeatInsuredFlg = true;
            }
        }

        if ($biMeg and preg_match('/终保日期：([\d\s:-]+)/', $biMeg, $matches)) {
            if ($matches[1] ?? '') {
                $biStartData = date('Y-m-d H:i:s', strtotime($matches[1]));
                $this->printer->repeatInsurePrinter['biStartData'] = $biStartData;
                $this->printer->repeatInsuredFlg = true;
            }
        }
    }

    public function buildCoverage($isEnergy, array $orgList)
    {
        $orgList = array_combine(array_column($orgList, 'coverageCode'), $orgList);

        foreach ($this->printer->coverageList as &$coverage) {
            /*** @var $coverage PremiumCoveragePrinter */

            if ($isEnergy) {
                $coverage->coverageCode = 'C1' . substr($coverage->coverageCode, 2, 4);
                $coverage->coverageName = IDockEnums::$coverageCode[$coverage->coverageCode];
            }

            if (!$coverage->amount) {
                $coverage->amount = $orgList[$coverage->coverageCode]->amount ?? '';
            }

            $coverage->quantity = $orgList[$coverage->coverageCode]->quantity ?? '';
            $coverage->unitAmount = $orgList[$coverage->coverageCode]->unitAmount ?? '';
            //服务次数回写
            $coverage->serviceTimes = $orgList[$coverage->coverageCode]->serviceTimes ?? '';
        }
    }

    private function setNonCarInfo($data)
    {
        $printer = new NonCarInquiryPrinter();
        $single = new NonCarInquirySinglePrinter();
        $single->productCode = $this->productCode;
        $single->planCode = $this->planCode;
        $single->inquiryNo = $data['subpolicyno'] ?? '';
        $printer->noCarInquirySingleAppend($single);
        $printer->totalPremium = $data['trialpremium'] ?? '0';
        $this->printer->nonCarInquiryPrinter = $printer;
    }
}
